package com.controller;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

@Controller
public class HelloController {
	// extends AbstractController {

	// @Override
	// protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
	// HttpServletResponse arg1) throws Exception {
	// ModelAndView modelAndView = new ModelAndView("hello.jsp");
	// modelAndView.addObject("msg", "Hello,Spring MVC!");
	// return modelAndView;
	// }

	@RequestMapping("/hello")
	public ModelAndView hello() {
		ModelAndView modelAndView = new ModelAndView("hello");// 逻辑视图名
		modelAndView.addObject("msg", "Hello,Spring MVC!");
		modelAndView.setViewName("hello");
		return modelAndView;
	}

//	@RequestMapping(
//			value = "/param",
//			method = RequestMethod.GET, //post请求
//			params = "username" //请求携带的参数
//			)
	@GetMapping("/param") //@RequestMapping(method = RequestMethod.GET)
//	@PostMapping("/param")
	public String param(
			@RequestParam(required = false,defaultValue = "无") String username,
			Model model,//优先使用
			HttpServletRequest request,//request
			HttpServletResponse response,
			HttpSession session) {
		System.out.println("username:" + username);
		
//		model.addAttribute("msg", "使用Model设置域的值");
		request.setAttribute("msg", "使用request设置域的值");
		return "hello";//逻辑视图
	}

	// 一个方法对应一个请求url
}
